package com.propertypoint.persitence.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.sql.*;

import com.propertypoint.util.DBUtil;
import com.propertypoint.presentation.model.Advertiesment;

/* CREATE  TABLE `propertypoint`.`agent_advertiesment` (
  `id` MEDIUMINT NOT NULL AUTO_INCREMENT ,
  `status` VARCHAR(25) NULL DEFAULT 'PENDING' ,
  `published_date` TIMESTAMP NOT NULL ,
  `end_date` TIMESTAMP NOT NULL ,
 */

public class AdminDAOImpl implements AdminDAO {

	private static final String SELECT_ADS="select * from agent_advertiesment";
	private static final String UPDATE_AD = "UPDATE agent_advertiesment SET status=? WHERE id=?";
	
	@Override
	public List<Advertiesment> getAds(){
		List<Advertiesment> L = new ArrayList<Advertiesment>();
		Connection con = null;
		try {
			con = DBUtil.getDBUtil().getConnection();
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(SELECT_ADS);
			while (rs.next()){
				Advertiesment ad = new Advertiesment();
				ad.setId(rs.getString("id"));
				ad.setPublishedDate(rs.getDate("published_date"));
				ad.setEndDate(rs.getDate("end_date"));
				ad.setStatus(rs.getString("status"));
				L.add(ad);
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//TEST dummy code
		/*
		Advertiesment ad = new Advertiesment();
		ad.setId("1");
		Date d = new Date(2010,10,8);
		ad.setPublishedDate(d);
		ad.setEndDate(d);
		ad.setStatus("APPROVED");
		L.add(ad);
		
		Advertiesment ad1 = new Advertiesment();
		ad1.setId("2");
		Date d1 = new Date(2010,10,8);
		ad1.setPublishedDate(d1);
		ad1.setEndDate(d1);
		ad1.setStatus("PENDING");
		L.add(ad1);*/
		
		
		return L;
	}

	@Override
	public void updateAdsStatus(int id, String status) {
		Connection con = null;
		try {
			con = DBUtil.getDBUtil().getConnection();
			PreparedStatement preparedStatement = con.prepareStatement(UPDATE_AD);

			preparedStatement.setString(1, status);
			preparedStatement.setInt(2, id);
			preparedStatement.executeUpdate();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
